﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.DirectoryServices;
using System.Data;
using System.Text;
using System.Web.Script.Services;
using ADExchangeLib;

namespace WebApplication2
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {   
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public string WebGetUser(string sUserName)
        {
            ADMethods AD = new ADMethods();
            string rs = "";
            DirectoryEntry userEntry = AD.GetUser(sUserName);
            if(userEntry!=null)
            {
                rs = userEntry.Path;
            }
            return rs;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public string WebGetOU(string sOUName, string sParentLocation)
        {
            ADMethods AD = new ADMethods();
            string rs = "";
            DirectoryEntry userEntry = AD.GetOU(sOUName,sParentLocation);
            if (userEntry != null)
            {
                rs = userEntry.Path;
            }
            else
            {
                rs = "fail to get ou";
            }
            return rs;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public string WebSetPassWord(string sUserName, string sNewPassword)
        {
            ADMethods AD = new ADMethods();
            string msg = "";
            AD.SetUserPassword(sUserName,sNewPassword,out msg);
            return msg;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public bool WebIsAccountActive(string sUserName)
        {
            ADMethods AD = new ADMethods();
            bool res;
            res=AD.IsAccountActive(sUserName);
            return res;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public string WebCreateNewUser(string sCN,string sParentLocation)//创建用户
        {
            ADMethods AD = new ADMethods();
            DirectoryEntry DE = AD.CreateNewUser(sCN, sParentLocation);
            if(DE!=null)
            {
                return DE.Path;
            }
            else
            {
                return "fail to create user";
            }
            
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public string  WebDeleteUser(string sUserName)//删除用户
        {
            ADMethods AD = new ADMethods();
            string msg= AD.DeleteUser(sUserName);
            return msg;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public string WebSetProperty(string sUserName, string sPropertyName, string sPropertyValue)
        {
            ADMethods AD = new ADMethods();
            string rs = "";
            DirectoryEntry userEntry = AD.GetUser(sUserName);
            if (userEntry != null)
            {
                byte[] bPropertyValue = System.Text.Encoding.Default.GetBytes(sPropertyValue);
                AD.SetProperty(userEntry, sPropertyName, bPropertyValue);
                return "success to  set property";
            }
            else
            {
                return "fail to set property";
            }
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public string WebGetProperty(string sUserName, string sPropertyName)
        {
            ADMethods AD = new ADMethods();
            string rs = "";
            DirectoryEntry userEntry = AD.GetUser(sUserName);
            if (userEntry != null)
            {
                rs=AD.GetProperty(userEntry, sPropertyName);
                return rs;
            }
            else
            {
                return string.Empty;
            }
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]

        public string WebCreateOU(string sOUName,string sParentLocation)
        {
            ADMethods AD = new ADMethods();
            DirectoryEntry userEntry = AD.CreateOU(sOUName, sParentLocation);
            if(userEntry!=null)
            {
                return userEntry.Path;
            }
            else
            {
                return "fail to createou";
            }
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]

        public string  WebEnableUserAccount(string sUserName)
        {
            ADMethods AD = new ADMethods();
            AD.EnableUserAccount(sUserName);
            return "enable user account success";
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public string WebSetOUName(string sOldOUName, string sNewOUName, string sParentLocation)
        {
            ADMethods AD = new ADMethods();
            string msg;
            msg = AD.SetOUName(sOldOUName, sNewOUName, sParentLocation);
            return msg;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public string WebDeleteOU(string sOUName,  string sParentLocation)
        {
            ADMethods AD = new ADMethods();
            string msg;
            msg = AD.DeleteOU(sOUName, sParentLocation);
            return msg;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public string WebGetUserProperty(string sUserName, string sPropertyName)
        {
            ADMethods AD = new ADMethods();
            string msg;
            msg = AD.GetUserProperty(sUserName, sPropertyName);
            return msg;
        }






    }
}
